<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="../vue.js"></script>
  </head>

  <body>
    <div id="app">
      <h1>{{msg}}</h1>
      <!-- 双向数据绑定 -->
      <input type="text" v-model="search" />
      <!-- 遍历新数组 -->
      <ul>
        <li v-for="item of newPerson" :key="item.id">
          {{item.name}}——{{item.age}}
        </li>
      </ul>
    </div>
    <script>
      Vue.config.productionTip = false;
      const vm = new Vue({
        el: "#app",
        data() {
          return {
            msg: "hello vue!",
            search: "",
            person: [
              {
                id: "001",
                name: "laowang",
                age: 18,
              },
              {
                id: "002",
                name: "xiaowang",
                age: 12,
              },
              {
                id: "003",
                name: "dawang",
                age: 33,
              },
              {
                id: "004",
                name: "xiaohuang",
                age: 6,
              },
            ],
            newPerson: [],
          };
        },
        //监听配置项
        watch: {
          //监听的属性
          search: {
            // 立即监听
            immediate: true,
            handler(newValue) {
              this.newPerson = this.person.filter((item) => {
                // 字符串方法includes 用于检查字符串中是否有特定的字符串。
                //存在返回ture 不存在返回false
                return item.name.includes(newValue);
              });
            },
          },
        },
        methods: {},
      });
    </script>
  </body>
</html>
